var apiManage = {
	

	//获取officeId
	getOfficeId:function(){
		var officeId= localStorage.getItem("officeId");
		if(officeId){
			return officeId;
		}
	},
	
	//获取companyId
	getCompanyId:function(){
		var companyId= localStorage.getItem("companyId");
		if(companyId){
			return companyId;
		}
	},
	
	//获取userid
	getUserid:function(){
		var userid= localStorage.getItem("userid");
		if(userid){
			return userid;
		}
	},
	
	//获取username 
	getUsername:function(){
		var username= localStorage.getItem("username");
		if(username){
			return username;
		}
	},
	//获取登录成功后返回的密钥
	getSecPwd:function(){
		var sec_pwd= localStorage.getItem("sec_pwd");
		if(sec_pwd){
			return sec_pwd;
		}
	},
		//获取当前时间戳
	getNow:function () {
		var timestamp=new Date().getTime();
		return timestamp;
	},
	
	//获取当前时间 并格式化
	getNowYYMMDD:function(){
		var date = new Date();
	    var seperator1 = "-";
	    var seperator2 = ":";
	    var month = date.getMonth() + 1;
	    var strDate = date.getDate();
	    var hours = date.getHours();
	    var minutes = date.getMinutes();
		var seconds = date.getSeconds();
		
	    if (month >= 1 && month <= 9) {
	        month = "0" + month;
	    }
	    if (strDate >= 0 && strDate <= 9) {
	        strDate = "0" + strDate;
	    }
	    
	    if (hours >= 0 && hours <= 9) {
	        hours = "0" + hours;
	    }
	    if (minutes >= 0 && minutes <= 9) {
	        minutes = "0" + minutes;
	    }
	    if (seconds >= 0 && seconds <= 9) {
	        seconds = "0" + seconds;
	    }
	        
	    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
//	            + " " + hours + seperator2 + minutes
//	            + seperator2 + seconds;


	    return currentdate;
	},
	
	//获取当前时间 并格式化
	getNowFormat:function(){
		var date = new Date();
	    var seperator1 = "-";
	    var seperator2 = ":";
	    var month = date.getMonth() + 1;
	    var strDate = date.getDate();
	    var hours = date.getHours();
	    var minutes = date.getMinutes();
		var seconds = date.getSeconds();
		
	    if (month >= 1 && month <= 9) {
	        month = "0" + month;
	    }
	    if (strDate >= 0 && strDate <= 9) {
	        strDate = "0" + strDate;
	    }
	    
	    if (hours >= 0 && hours <= 9) {
	        hours = "0" + hours;
	    }
	    if (minutes >= 0 && minutes <= 9) {
	        minutes = "0" + minutes;
	    }
	    if (seconds >= 0 && seconds <= 9) {
	        seconds = "0" + seconds;
	    }
	        
	    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
	            + " " + hours + seperator2 + minutes
	            + seperator2 + seconds;
	    return currentdate;
	},
	
	
	//获取当前时间 并格式化
	getNowYYMMDDHHMMSS:function(){
		var date = new Date();
	    var seperator1 = "-";
	    var seperator2 = ":";
	    var month = date.getMonth() + 1;
	    var strDate = date.getDate();
	    var hours = date.getHours();
	    var minutes = date.getMinutes();
		var seconds = date.getSeconds();
		
	    if (month >= 1 && month <= 9) {
	        month = "0" + month;
	    }
	    if (strDate >= 0 && strDate <= 9) {
	        strDate = "0" + strDate;
	    }
	    
	    if (hours >= 0 && hours <= 9) {
	        hours = "0" + hours;
	    }
	    if (minutes >= 0 && minutes <= 9) {
	        minutes = "0" + minutes;
	    }
	    if (seconds >= 0 && seconds <= 9) {
	        seconds = "0" + seconds;
	    }
	        
	    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
	            +  seperator1 + hours + seperator1 + minutes+seperator1+seconds;
	            
	            console.log("currentdate---"+currentdate);
	            return currentdate;
	            
	            
	},
	
	//获取当前时间 并格式化
	getNowYYMMDDHHMM:function(){
		var date = new Date();
	    var seperator1 = "-";
	    var seperator2 = ":";
	    var month = date.getMonth() + 1;
	    var strDate = date.getDate();
	    var hours = date.getHours();
	    var minutes = date.getMinutes();
		var seconds = date.getSeconds();
		
	    if (month >= 1 && month <= 9) {
	        month = "0" + month;
	    }
	    if (strDate >= 0 && strDate <= 9) {
	        strDate = "0" + strDate;
	    }
	    
	    if (hours >= 0 && hours <= 9) {
	        hours = "0" + hours;
	    }
	    if (minutes >= 0 && minutes <= 9) {
	        minutes = "0" + minutes;
	    }
	    if (seconds >= 0 && seconds <= 9) {
	        seconds = "0" + seconds;
	    }
	        
	    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
	            + " " + hours + seperator2 + minutes
	            console.log("currentdate---"+currentdate);
	            return currentdate;
	            
	            
	            
	},
	
	//get方式获取
	get: function(url, param, success, err) {
		if(param != null) {
			url = this.makeUrl(url, param);
		}
		//localStorage.getItem("token");
//		console.log(localStorage.getItem("glasses-loginInfo")==null);return;
		var staffID=localStorage.getItem("glasses-loginInfo")==null?null:JSON.parse(localStorage.getItem("glasses-loginInfo")).staffID;
		console.log("staffID"+staffID);
//		console.log("staffID"+staffID);
//		url += "&staffID=" +staffID+ "&token=" + localStorage.getItem("token");
		console.log(url);

		var sets = {
			url: url,
			type: 'GET',
			backType: 'JSON',
			success: success,
			error: err
		};
		this.ajax(sets);
	},

	//普通参数方式提交
	post: function(url, params, success, err) {
		var sets = {
			url: url,
			type: 'POST',
			backType: 'JSON',
			data: params,
			success: success,
			error: err,
			ContentType: 'application/x-www-form-urlencoded'
		};
		this.ajax(sets);
	},

	//json字符串参数方式提交
	postByJson: function(url, params, success, err) {
		var sets = {
			url: url,
			type: 'POST',
			backType: 'JSON',
			data: params,
			success: success,
			error: err,
			ContentType: "application/json"
		};
		this.ajax(sets);
	},

	ajax: function(sets) {
		//		localStorage.setItem("token", "1");
		if(!sets) {
			sets = {
				url: null
			};
		}
		if(!sets.url) {
			return;
		}
		var async = 'async' in sets ? sets.async : true;
		sets.type = 'type' in sets ? sets.type.toUpperCase() : 'GET';
		sets.backType = 'backType' in sets ? sets.backType.toUpperCase() : 'HTML';
		sets.beforeSend = 'beforeSend' in sets ? sets.beforeSend : null;
		sets.complete = 'complete' in sets ? sets.complete : null;
		sets.success = 'success' in sets ? sets.success : function() {};
		sets.error = 'error' in sets ? sets.error : function(e) {
			console.log('ajax error : ' + JSON.stringify(e));
		}
		sets.ContentType = 'ContentType' in sets ? sets.ContentType : 'application/x-www-form-urlencoded';
		sets.header = 'header' in sets ? sets.header : false;
		var xhr = new window.XMLHttpRequest();
		xhr.open(sets.type, sets.url, async);
		if(typeof(xhr) == 'undefined') {
			if(sets.error) {
				sets.error(_lang.xhrError);
			}
			return;
		}
		if(sets.header) {
			for(var i = 0; i < sets.header.length; i++) {
				xhr.setRequestHeader(sets.header[i][0], sets.header[i][1]);
			}
		}
		xhr.setRequestHeader("sessionId", "123456");
var staffID=localStorage.getItem("glasses-loginInfo")==null?null:JSON.parse(localStorage.getItem("glasses-loginInfo")).staffID;
			xhr.setRequestHeader("staffID", staffID);
			xhr.setRequestHeader("token", localStorage.getItem("token"));
		xhr.timeout = 'timeout' in sets ? sets.timeout : 0;
		if(sets.beforeSend) {
			sets.beforeSend();
		}

		xhr.onreadystatechange = function() {
			if(xhr.readyState == 4) {
				if(sets.complete) {
					sets.complete();
				}
				if(xhr.status == 200) {
					if(sets.backType == 'HTML') {
						sets.success(xhr.responseText);
					} else if(sets.backType == 'JSON') {
						//console.log(xhr.responseText);
						try {
							var ret = JSON.parse(xhr.responseText);
							
								sets.success(ret);
						
						} catch(e) {
							console.log("解析错误," + xhr.responseText);
							//hui.toast("返回数据错误,"  +  xhr.responseText)
							sets.error("网络错误," + e.message);
						}
					}
				}
			}
		}
		xhr.ontimeout = function() {
			sets.error(_lang.ajaxTimeout);
		}
		if(sets.error) {
			xhr.onerror = function(e) {
				sets.error(e);
			}
		}

		if(sets.type == 'POST') {
			var pd = '';
//			sets.data.staffID= JSON.parse(localStorage.getItem("glasses-loginInfo")).staffID;
//			sets.data.token =localStorage.getItem("token");
//			var staffID=localStorage.getItem("glasses-loginInfo")==null?null:JSON.parse(localStorage.getItem("glasses-loginInfo")).staffID;
//			xhr.setRequestHeader("staffID", staffID);
//			xhr.setRequestHeader("token", localStorage.getItem("token")); 
			if(sets.ContentType == 'application/x-www-form-urlencoded') {
				for(var k in sets.data) {
					pd += encodeURIComponent(k) + '=' + encodeURIComponent(sets.data[k]) + '&';
				}
				pd = pd.substr(0, pd.length - 1);
				//console.log(pd);
				xhr.setRequestHeader('Content-Type', sets.ContentType);
				xhr.send(pd);
			} else {
				xhr.setRequestHeader('Content-Type', sets.ContentType);
				xhr.send(JSON.stringify(sets.data));
			}
			console.log("zz" + JSON.stringify(sets.data));
		} else {
			xhr.send();
			console.log("xx" + sets.url);
		}
console.log("xx" + sets.url);
	},

	makeUrl: function(url, param) {
		var returnStr = "";
		for(var key in param) {
			if(returnStr == "") {
				returnStr += "?";
			} else {
				returnStr += "&";
			}

			returnStr += key;
			returnStr += "=";
			returnStr += param[key];
		};
		return url + returnStr;
	}
	
	,
	getMenuConfig:function(){//获取用户菜单配置
		var menuMap = localStorage.getItem("menuMap");
		console.log("menuMap---"+menuMap);
		var menuMapObj = JSON.parse(menuMap);
		
		if(menuMapObj["app:resterminal:index:domainconfig"]!=null){
			localStorage.setItem("domainconfig","true");
			console.log("有 域名设置权限");
		}
		else{
			localStorage.setItem("domainconfig","false");
			console.log(" 无 域名设置权限");
		}
		
		

           
//

//
//巡检管理:app:check:index
//任务列表:app:check:index:checktasklist
//签到:app:check:index:checkregister
//新建工单:app:check:index:workcreate
//巡检记录上传:app:check:index:checkrecordupload
//隐患上传:app:check:index:checktroubleupload
//历史巡检:app:check:index:historycheck
//
//
//告警展示:app:alarm:index
//
//主动定位:app:location:index
//
//GIS终端:app:gisterminal:index

//域名设置 app:resterminal:index:domainconfig

	

	//设置主菜单优先级并返回菜单跳转的页面地址
    function settingIndex(title){
    	//title改为key
    	
    	var index=1;
    	var url ="";
    	
    		if(title=='app:location:index'){//主动定位
				index=9;
				url="/modules/login/gd_location.html";
			}
			else if(title=='app:check:index'){//巡检管理
				index=4;
				url="/modules/inspection/inspection.html";
			}
			else if(title=='app:resterminal:index'){//终端管理
				index=3;
				url="/modules/terminal/terminal.html";
			}
			else if(title=='app:work:index'){//工单管理
				index=2;
				url="/modules/work/job_main.html";
			}
			else if(title=='app:alarm:index'){//告警展示
				index=5;
				url="/modules/alerm/alarm_list.html";
			}
			else if(title=='app:gisterminal:index'){//"GIS终端
				index=1;
				url="/modules/gisTerminal/gisTerminal_map.html";
			}
			else if(title=='app:operationanalysis:index'){//运营分析
				index=6;
				url="/modules/analysis/analysis_main.html";
			}
			else{
				
			}
			
			var cell = new Object();
			cell.index=index;
			cell.url=url;
			//console.log("goParam-----"+ index);
			return cell;
    }
 
	//获取最终的菜单选项
    var config=[];
    
    var childMenu=[];//二级菜单
	for(var key in menuMapObj){
		if(key=="app:index"){
			continue;
		}
		if(key.indexOf("index:")!= -1 ){//是二级菜单项则过滤
			childMenu.push({key:key,value:menuMapObj[key]});
			continue;
		}
		var fdStart = key.indexOf("app:");
		if(fdStart == 0){
			
			
//			var cell = settingIndex(menuMapObj[key]);
		var cell = settingIndex(key);
		   var index=	cell.index;
		   var url = cell.url;
		   
		 config.push({key:key,value:menuMapObj[key],index:index,url:url});
		}else if(fdStart == -1){
		}
		else{
		}
		

	}
//	console.log("config----"+JSON.stringify(config));
//		console.log("childMenu----"+JSON.stringify(childMenu));
		settingChild()
		
		//设置优先级并返回二级菜单跳转的页面地址
    function settingChild(){
    	//title改为key
    	 	var worksArray = new Array();
    	 	var zdArray = new Array();
    	 	var xjArray = new Array();
    	 	var yyArray = new Array();

		for (var i=0;i<childMenu.length;i++) {
			
				var cell =new Object();
				var key = childMenu[i]["key"];
				var value = childMenu[i]["value"];
          if(key.indexOf('app:work:index:') !=-1 ){//工单二级菜单
	         
	          	if(key=='app:work:index:create'){//新建工单
					cell.index=1;
					cell.icon="&#xe62c;";
					cell.url="/modules/work/work_add.html";
					cell.title=value;
					worksArray.push(cell);
				}
				else if(key=='app:work:index:worklist'){//工单列表
					cell.index=2;
					cell.icon="&#xe648;";
					cell.title=value;

					cell.url="/modules/work/work_list.html";
					worksArray.push(cell);
				}
				else if(key=='app:work:index:worksummarizing'){//工单列表图
					cell.index=3;
					cell.icon="&#xe60e;";
					cell.url="/modules/work/job_list_img.html";
					cell.title=value;

					worksArray.push(cell);
				}
				else if(key=='app:work:index:historicalwork'){//历史工单
					cell.index=4;
					cell.title=value;

					cell.icon="&#xe61c;";
					cell.url="/modules/work/work_history_list.html";
					worksArray.push(cell);
				}
				else if(key=='app:work:index:workview'){//工单视图
					cell.index=5;
					cell.title=value;

					cell.icon="&#xe61c;";
					cell.url="/modules/work/work_view.html";
					worksArray.push(cell);
				}
				else{
				}
				
				
			
          }

      
          else if(key.indexOf("app:check:index:")!=-1){//巡检管理2级菜单
          		if(key=='app:check:index:checktasklist'){//任务列表:
							cell.index=1;
							cell.icon="&#xe642;";
							cell.title=value;
							cell.url="/modules/inspection/inspection_list.html";
							xjArray.push(cell);
						}
          			else if(key=='app:check:index:checkregister'){//签到：
							cell.index=2;
							cell.icon="&#xe655;";
							cell.title=value;
							cell.url="";
							xjArray.push(cell);
						}
          		
          			else if(key=='app:check:index:workcreate'){//新建工单：
							cell.index=3;
							cell.icon="&#xe62c;";
							cell.title=value;
							cell.url="/modules/work/work_add.html";
							xjArray.push(cell);
						}
					else if(key=='app:check:index:checkrecordupload'){//巡检记录上传：
							cell.index=4;
							cell.icon="&#xe60a;";
							cell.title=value;
							cell.url="";
							xjArray.push(cell);
						}
					else if(key=='app:check:index:checktroubleupload'){//隐患上传:：
							cell.index=5;
							cell.icon="&#xe6a1;";
							cell.title=value;
							cell.url="/modules/inspection/risk.html";
							xjArray.push(cell);
						}
					else if(key=='app:check:index:historycheck'){//历史巡检
							cell.index=6;
							cell.icon="&#xe685;";
							cell.title=value;
							cell.url="/modules/inspection/inspection_history.html";
							xjArray.push(cell);
						}
					
					else{
							
					}
          }
								
								
			     else if(key.indexOf('app:resterminal:index:') !=-1){//终端管理2级菜单
			     	   if(key=='app:resterminal:index:unlocking'){//开锁
			     	   		cell.index=1;
							cell.icon="&#xe606;";
							cell.url="";
							cell.title=value;
							zdArray.push(cell);
			     	   }
			     	
					    else if(key=='app:resterminal:index:made'){//报装：
							cell.index=2;
							cell.icon="&#xe6e9;";
							cell.url="/modules/terminal/terminal_map.html";
							cell.title=value;
							zdArray.push(cell);
						}
						else if(key=='app:resterminal:index:reservation'){//施工预约：
							cell.index=3;
							cell.icon="&#xe658;";
							cell.title=value;
							cell.url="/modules/terminal/order_list.html";
							zdArray.push(cell);
						}
						else if(key=='app:resterminal:index:terminalconfig'){//终端设置：
							cell.index=4;
							cell.icon="&#xe639;";
							cell.title=value;
//							hui.open("searchDevices.html?date2="+timestamp, {}, true, {});
							
							cell.url="/modules/terminal/searchDevices.html";
							zdArray.push(cell);
						}
						else if(key=='app:resterminal:index:terminalupdate'){//终端升级：
							cell.index=5;
							cell.icon="&#xe61e;";
							cell.title=value;
							cell.url="";
							zdArray.push(cell);
						}
			     		else if(key=='app:resterminal:index:conifg'){//设置：
							cell.index=6;
							cell.icon="&#xe602;";
							cell.title=value;
							cell.url="";
							zdArray.push(cell);
						}
			     		else if(key=='app:resterminal:index:terminalconnect'){//终端连接：
							cell.index=7;
							cell.icon="&#xe65a;";
							cell.title=value;
							cell.url="";
							zdArray.push(cell);
						}
			    		else if(key=='app:resterminal:index:terminaldormant'){//终端休眠：
							cell.index=8;
							cell.icon="&#xe607;";
							cell.title=value;
							cell.url="";
							zdArray.push(cell);
						}
						
						else{
							
						}
			     }
			     
//			     今日告警处理率:app:operationanalysis:index:alarmtodayprocessrate
//告警统计分析:app:operationanalysis:index:alarmstatisticalanalysis
			     
			    else if(key.indexOf("app:operationanalysis:index:")!=-1){//运营分析2级菜单
          		if(key=='app:operationanalysis:index:alarmtodayprocessrate'){//今日告警处理率
							cell.index=1;
							cell.icon="&#xe642;";
							cell.title=value;
							cell.url="/modules/analysis/todayAlarm.html";
							yyArray.push(cell);
						}
          			else if(key=='app:operationanalysis:index:alarmstatisticalanalys'){//告警统计分析
							cell.index=2;
							cell.icon="&#xe655;";
							cell.title=value;
							cell.url="/modules/analysis/alarm_chart.html";
							yyArray.push(cell);
						}
          		
					
					else{
							
					}
          }
			     
			     
			     
			     else{
			     	
			     }
		
    }
		
		
		
	yyArray.sort(function(a,b){
            return a.index-b.index
	}
	);
	
	xjArray.sort(function(a,b){
            return a.index-b.index
	}
	);
			zdArray.sort(function(a,b){
            return a.index-b.index
	}
	);
		//按优先级排序
	worksArray.sort(function(a,b){
            return a.index-b.index
	}
	);
			
			localStorage.setItem("worksArray",JSON.stringify(worksArray));
//			console.log("工单二级菜单列表:---"+JSON.stringify(worksArray));
			localStorage.setItem("zdArray",JSON.stringify(zdArray));
//			console.log("终端二级菜单列表:---"+JSON.stringify(zdArray));
			localStorage.setItem("xjArray",JSON.stringify(xjArray));
//			console.log("巡检二级菜单列表:---"+JSON.stringify(xjArray));
			localStorage.setItem("yyArray",JSON.stringify(yyArray));

		}
    	

			var timestamp=new Date().getTime();


   	
		
		

	//按优先级排序
	config.sort(function(a,b){
            return a.index-b.index
	}
	);
	
	return config;
	
	}

}